﻿#region

using System.Collections.Generic;
using System.Linq;
using System.Web;
using CuongHanh.Models;
using CuongHanh.Service;

#endregion

namespace CuongHanh.Context
{
    public class CartAndPaymentContext
    {
        #region Trang suc

        public static void AddToCartOfGem(HttpSessionStateBase session,
                OrderDetailForGem model)
        {
            List<OrderDetailForGem> list = new List<OrderDetailForGem>();
            if(session[AppUtil.GEM_BAG] != null)
            {
                list = (List<OrderDetailForGem>) session[AppUtil.GEM_BAG];
            }

            OrderDetailForGem current = list.FirstOrDefault(c => c.ParentStyleId == model.ParentStyleId && c.ParentMaterialId == model.ParentMaterialId && c.ParentStoneId == model.ParentStoneId && c.ChildStyleId == model.ChildStyleId && c.ChildMaterialId == model.ChildMaterialId && c.ChildStoneId == model.ChildStoneId && c.ParentSize == model.ParentSize);
                   
            if(current != null)
            {
                current.Count++;
            }
            else
            {
                model.Id = GetMaxId(list);
                model.Count = 1;
                MyStyleService.UpdateModelOrderDetailForGem(ref model);
                list.Add(model);
                session[AppUtil.GEM_BAG] = list;
            }
        }

        private static int GetMaxId(IEnumerable<OrderDetailForGem> list)
        {
            int maxId = 0;
            foreach (OrderDetailForGem item in list)
            {
                if(item.Id > maxId)
                    maxId = item.Id;
            }
            return ++maxId;
        }

        public static List<OrderDetailForGem> GetBagInfoOfGem(HttpSessionStateBase session)
        {
            List<OrderDetailForGem> list = new List<OrderDetailForGem>();
            if(session[AppUtil.GEM_BAG] != null)
            {
                list = (List<OrderDetailForGem>) session[AppUtil.GEM_BAG];
            }
            return list;
        }

        public static int GetCountItemInBagOfGem(HttpSessionStateBase session)
        {
            List<OrderDetailForGem> list = new List<OrderDetailForGem>();
            if (session[AppUtil.GEM_BAG] != null)
            {
                list = (List<OrderDetailForGem>)session[AppUtil.GEM_BAG];
            }
            return list.Count;
        }

        public static bool CheckCartInfo(HttpSessionStateBase session)
        {
            if(session[AppUtil.GEM_BAG] == null)
                return false;
            List<OrderDetailForGem> list = (List<OrderDetailForGem>)session[AppUtil.GEM_BAG];
            return list.Count > 0;
        }


        public static void UpdateItemCountOfGem(HttpSessionStateBase session,int id, int count)
        {
            List<OrderDetailForGem> list = new List<OrderDetailForGem>();
            if(session[AppUtil.GEM_BAG] != null)
            {
                list = (List<OrderDetailForGem>) session[AppUtil.GEM_BAG];
            }
            var item = list.FirstOrDefault(c => c.Id == id);
            if(item != null)
            {
                item.Count = count;
            }
        }

        public static void RemoveCartItemOfGem(HttpSessionStateBase session, int id)
        {
            List<OrderDetailForGem> list = new List<OrderDetailForGem>();
            if (session[AppUtil.GEM_BAG] != null)
            {
                list = (List<OrderDetailForGem>)session[AppUtil.GEM_BAG];
            }
            var item = list.FirstOrDefault(c => c.Id == id);
            if (item != null)
            {
                list.Remove(item);
            }
        }
        #endregion
    }
}
